package com.stone.im.geteway.tcp;

import com.stone.im.geteway.dispather.DispatcherInstance;
import com.stone.im.geteway.dispather.DispatcherInstanceManger;
import com.stone.im.protobuf.proto.AuthenticateRequestProto;
import com.stone.im.protobuf.proto.AuthenticateResponseProto;
import com.stone.im.protobuf.proto.MessagePushResponseProto;
import com.stone.im.protobuf.proto.MessageSendRequestProto;

/**
 * 请求处理组件
 */
public class RequestHandle {

    static class Singleton{
        static RequestHandle instance = new RequestHandle();
    }

    public static RequestHandle getInstance(){
        return Singleton.instance;
    }

    /**
     * 认证请求处理逻辑
     * @return
     */
    public void authenticate(AuthenticateRequestProto.AuthenticateRequest authenticateRequest){
        //主要是将请求发送给分发系统
        //随机从分发系统的多个实例中获取一个实例
        DispatcherInstanceManger dispatcherInstanceManger = DispatcherInstanceManger.getSingleton();
        DispatcherInstance dispatcherInstance = dispatcherInstanceManger.chooseDispatcherInstance();
        dispatcherInstance.authenticate(authenticateRequest);

        System.out.println("向随机挑选的分发系统发起认证");
    }

    /**
     * 发送消息
     * @param messageSendRequest
     */
    public void sendMessage(MessageSendRequestProto.MessageSendRequest messageSendRequest){

        DispatcherInstanceManger dispatcherInstanceManger = DispatcherInstanceManger.getSingleton();
        DispatcherInstance dispatcherInstance = dispatcherInstanceManger.chooseDispatcherInstance();
        dispatcherInstance.sendMessage(messageSendRequest);
        System.out.println("向随机挑选的分发系统发送单聊消息");
    }

    /**
     * 消息推送的响应
     * @param messagePushResponse
     */
    public void pushMessageResponse(MessagePushResponseProto.MessagePushResponse messagePushResponse){
        DispatcherInstanceManger dispatcherInstanceManger = DispatcherInstanceManger.getSingleton();
        DispatcherInstance dispatcherInstance = dispatcherInstanceManger.chooseDispatcherInstance();
        dispatcherInstance.pushMessageResponse(messagePushResponse);
        System.out.println("向随机挑选的分发系统发送单聊消息");
    }
}
